package com.company.chatting;

import com.company.utils.JSonUtil;
import com.company.utils.MyException;
import org.codehaus.jackson.map.util.JSONPObject;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @Auther Xue
 * @Date 2021/4/28
 * @Version 1.0
 * @Desc
 */
public class ServerSend implements Runnable {
    private Socket socket;

    public ServerSend(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            Scanner scanner = new Scanner(System.in);
            while (true){
                System.out.println("请输入");
                String str = scanner.nextLine();
                Map obj = new HashMap();
                obj.put("type","chat");
                obj.put("msg",str);
                oos.writeObject(JSonUtil.obj2String(obj));
                oos.flush();
            }

        } catch (IOException e) {
            MyException.logExceptionMessage(e);
        }finally {
            try {
                socket.close();
            } catch (IOException e) {
                MyException.logExceptionMessage(e);
            }
        }
    }
}
